package com.itestor.iptv.modules.common.actions;

import java.util.ArrayList;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.itestor.iptv.global.GlobalConstant;
import com.itestor.iptv.modules.common.service.UserService;
import com.itestor.iptv.modules.common.utils.CommonConstant;
import com.itestor.iptv.modules.common.vo.User;
import com.itestor.iptv.global.exception.WebApplicationException;
import com.itestor.iptv.global.utils.LoggerUtils;


/**
 * 
 * @author Mr.li
 *
 */
public class UserModifyAction extends Action {

	/**
	 * Logger for this class.
	 */
	private static final Logger logger = Logger.getLogger(UserModifyAction.class);

	/**
	 * UserService.
	 */
	private UserService userService;

	/**
	 * @param mapping struts mapping
	 * @param request request
	 * @param form action form
	 * @param response response
	 * @return action forward
	 * @throws Exception any exception
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String username = request.getParameter("username");
		
		if (StringUtils.isEmpty(username)) {
			throw new WebApplicationException("global.error.submit", GlobalConstant.PROPERTIES_BUNDLE);
		}
		
		User user = userService.get(username);
		if (user != null) {
			String password = request.getParameter("password");
			if(!StringUtils.isEmpty(password)){
				String oldpassword = request.getParameter("oldpassword");
				if (!oldpassword.equals(user.getPassword())) {
					throw new WebApplicationException("system.error.password", CommonConstant.PROPERTIES_BUNDLE);
				}
				user.setPassword(password);
			}
			String name = request.getParameter("name");
			String role = request.getParameter("role");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
			String mobile = request.getParameter("mobile");

			user.setRealname(name);
			Collection<String> rolenames = new ArrayList<String>();
			rolenames.add(role);
			user.setRolenames(rolenames);
			user.setEmail(email);
			user.setPhone(phone);
			user.setMobile(mobile);

			request.setAttribute(CommonConstant.REQUEST_ATTR_USER, user);
			
			LoggerUtils.logActionInfo("Modify the user - username: " + username, logger, request);

			userService.modify(user, (User) request.getSession().getAttribute(CommonConstant.SESSION_ATTR_USER));
		}
		return mapping.findForward(GlobalConstant.FORWARD_SUCCESS);
	}

	/**
	 * @param pUserService the userService to set
	 */
	public void setUserService(UserService pUserService) {
		userService = pUserService;
	}
}
